package com.justyh.ssm.util;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Chejianer on 2016/9/2.
 */
public class ExcelBestColumn {
    public static void main(String argus[]) throws Exception{
        //构造数据，有两行
        List<String> row1=new ArrayList<String>();    row1.add("最合适列宽");    row1.add("这个基本可以实现");
        List<String> row2=new ArrayList<String>();    row2.add("Best Column Width");    row2.add("Haha");
        List list=new ArrayList();    list.add(row1);    list.add(row2);

        //写数据到Excel中
        WritableWorkbook book= Workbook.createWorkbook(new File("t.xls"));
        WritableSheet sheet=book.createSheet("测试",0);
        writeDataToSheet(sheet,list);
        book.write();
        book.close();
    }

    public static void writeDataToSheet(WritableSheet sheet,List<List<String>> list) throws Exception{
        int columnBestWidth[]=new  int[list.get(0).size()];    //保存最佳列宽数据的数组

        for(int i=0;i<list.size();i++){
            List<String> row=list.get(i);
            for(int j=0;j<row.size();j++){
                sheet.addCell(new Label(j,i,row.get(j)));

                int width=row.get(j).length()+getChineseNum(row.get(j));    ///汉字占2个单位长度
                if(columnBestWidth[j]<width)    ///求取到目前为止的最佳列宽
                    columnBestWidth[j]=width;
            }
        }

        for(int i=0;i<columnBestWidth.length;i++){    ///设置每列宽
            sheet.setColumnView(i, columnBestWidth[i]);
        }
    }

    public static int getChineseNum(String context){    ///统计context中是汉字的个数
        int lenOfChinese=0;
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");    //汉字的Unicode编码范围
        Matcher m = p.matcher(context);
        while(m.find()){
            lenOfChinese++;
        }
        return lenOfChinese;
    }
}

